package com.dai.zhenfujin.visitingcard.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.VisitingTypeAdpter;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.bean.TypeListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.view.SodukuGridView;
import com.dai.zhenfujin.visitingcard.SearchActiviy;
import com.dai.zhenfujin.visitingcard.adapter.VisitingCardAdapter2;
import com.dai.zhenfujin.visitingcard.app.ParticularsPageActivity;
import com.dai.zhenfujin.visitingcard.bean.VisitingCardBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * h
 */
public class Roomtivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView listView;
    private FrameLayout fl_img;
    private TextView tvName;
    private ImageView iv_no_msg;
    private View view, view_line;

    private List<VisitingCardBean> beanList;
    private VisitingCardAdapter2 adapter;

    private SodukuGridView rv_type;
    private VisitingTypeAdpter typeListAdapter;
    private int page = 1;
    private int number = 10;
    private SmartRefreshLayout refreshLayout;
    private ImageView title_search;

    private TextView tvNewHouse, tvHandHouse, tvRentsharing,
            tvRenovation, tvResidence, tvMaterial;
    private List<TypeListBean.TypelistBean> typeList = new ArrayList<>();
    private List<ProListBean.ProListBean2> proListBBean = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roomtivity);
        view = View.inflate(this, R.layout.fragment_visitingcard_head2, null);
        rv_type = view.findViewById(R.id.grid_view);
        iv_no_msg = findViewById(R.id.iv_no_msg);
        refreshLayout = findViewById(R.id.refreshLayout);
        initView();
        onRefresh();
        initRefresh();
    }


    private void onRefresh() {
        page = 1;
        getProductList(true);
    }

    private void initRefresh() {
        refreshLayout.setOnRefreshListener(refreshLayout -> refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1;
                getProductList(true);
                refreshLayout.finishRefresh();
                refreshLayout.resetNoMoreData();
            }
        }, 1500));

        refreshLayout.setOnLoadmoreListener(refreshlayout -> getProductList(false));
    }


    private void initView() {
        String name = getIntent().getStringExtra("name");
        listView = findViewById(R.id.room_listview);
        adapter = new VisitingCardAdapter2(this, proListBBean);
        listView.addHeaderView(view);
        listView.setAdapter(adapter);
        listView.setVerticalScrollBarEnabled(false);

        typeListAdapter = new VisitingTypeAdpter(this, typeList);
        rv_type.setNumColumns(5);
        rv_type.setAdapter(typeListAdapter);
        fl_img = findViewById(R.id.fl_img);
        view_line = findViewById(R.id.view_line);
        tvName = findViewById(R.id.title_textview);

        tvName.setText(name);
        fl_img.setOnClickListener(this);
        rv_type.setOnItemClickListener(this);

        title_search = findViewById(R.id.title_search);
        title_search.setVisibility(View.VISIBLE);
        title_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Roomtivity.this, SearchActiviy.class);
                startActivity(intent);
            }
        });
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (position > 0) {
                Intent intent = new Intent(this, ParticularsPageActivity.class);
                intent.putExtra("pro_id", proListBBean.get(position - 1).getPro_id());
                intent.putExtra("type_id", proListBBean.get(position - 1).type_id);
                startActivity(intent);
            }
        });

        //   getProductList();

        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("type_id", getIntent().getStringExtra("type_id"));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.TYPELIST, params, new CallBack<TypeListBean>() {
            @Override
            public void onSuccess(TypeListBean resultBean) {
                typeList.clear();
                typeList.addAll(resultBean.getTypeList());
                if (typeList.size() == 0){
                    view.setVisibility(View.GONE);
                }
                typeListAdapter.notifyDataSetChanged();
            }
        });
    }


    private void getProductList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("type_id", getIntent().getStringExtra("type_id"));
        params2.put("words", "");
        params2.put("page", page + "");
        params2.put("number", number + "");
        params2.put("loc_x", SPUtils.getString(this, "longitude"));
        params2.put("loc_y", SPUtils.getString(this, "latitude"));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.PRODUCTLIST, params, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean proListBean) {
                if (isRefresh) {
                    proListBBean.clear();
                    if (proListBean.getProList().size() == 0){
                        proListBBean.addAll(proListBean.getProList());
                        adapter.notifyDataSetChanged();
                        iv_no_msg.setVisibility(View.VISIBLE);
                        return;
                    }
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }
                if (proListBean.getProList().size() > 0) {
                    proListBBean.addAll(proListBean.getProList());
                    adapter.notifyDataSetChanged();
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                    if (page == 2) {
                        view_line.setVisibility(View.GONE);
                        iv_no_msg.setVisibility(View.VISIBLE);
                    }
                    // UIUtils.showToastSafe("暂无数据");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_img:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //gridview
        Intent intent = new Intent(this, MarryActivity.class);
        intent.putExtra("type_id", typeList.get(position).getT_id());
        intent.putExtra("name", typeList.get(position).getT_name());
        startActivity(intent);
    }
}
